<template>
  <div>
    <!-- 全屏播放器 -->
    <div class="player" v-if="SongDetail" v-show="showPlayer">
      <div
        class="bg"
        :style="'background-image: url(' + SongDetail.al.picUrl + ')'"
      ></div>
      <div class="shade"></div>
      <div class="view">
        <div class="button" @click.stop="showPlayer = false">
          <span class="iconfont icon-fanhui"></span>
        </div>
        <div class="title">
          <h2>{{ SongDetail.name }}</h2>
          <h3>
            <span v-for="(item, index) in SongDetail.ar" :key="index"
              >{{ item.name }}&nbsp;</span
            >
          </h3>
        </div>
        <div class="content">
          <div
            v-show="togglelyric"
            @click="togglelyric = false"
            :class="['img', 'play', { pause: iconplay }]"
          >
            <img :src="SongDetail.al.picUrl" alt="" />
          </div>
          <div class="lyrics" v-show="!togglelyric" @click="togglelyric = true">
            <ul :style="{ top: lyricTop }">
              <li
                v-for="(item, index) in lyric"
                :key="index"
                :class="{ con: currentRow == index }"
              >
                {{ item.text }}
              </li>
            </ul>
          </div>
        </div>
        <div class="shareviedo">
          <!-- 评论 -->
          <!-- <div is-link @click="showPopup" class="iconfont icon-pinglun"></div> -->
        </div>
        <div class="range">
          <div class="bar">
            <span class="timeLeft">{{ timeLeft | playerTime }}</span>
            <van-slider
              @change="sliderchange"
              v-model="value"
              inactive-color="#514E4C"
              bar-height="4px"
              active-color="#ee0a24"
            >
              <template #button>
                <div class="custom-button"></div>
              </template>
            </van-slider>
            <span class="timeRight">{{ timeRight | playerTime }}</span>
          </div>
        </div>
        <div class="buttons">
          <div
            v-if="playerType == 1"
            @click="changeType"
            class="iconfont icon-xunhuan1"
          ></div>
          <div v-else @click="changeType" class="iconfont icon-xunhuan"></div>
          <div
            class="iconfont icon-shangyishoushangyige"
            @click="startup"
          ></div>
          <!-- <div class="iconfont icon-04"></div> -->
          <div
            :class="['iconfont', iconplay ? 'icon-24gf-play' : 'icon-zanting']"
            @click.stop="toggleplay"
          ></div>
          <div class="iconfont icon-xiayigexiayishou" @click="lowerFun"></div>
          <div is-link @click="showPopup" class="iconfont icon-pinglun"></div>
          <!-- <div
            @click="dianzis"
            :class="['iconfont', dianzan ? 'icon-kongxin' : 'icon-shixin']"
          > 
          </div> -->
          
        </div>
      </div>
    </div>
    <!-- 弹出评论 -->
    <van-popup
      :overlay="false"
      v-model="show"
      closeable
      close-icon-position="top-right"
      position="bottom"
      :style="{ height: '70%' }"
    >
      <div class="commentsvie">
        <div class="mvcomments">
          <div class="plq">评论区</div>
          <!-- <div class="tuijian">推荐</div> -->
        </div>
        <!-- 每一个评论 -->
        <div
          v-for="(item, index) in commenlist"
          :key="index"
          class="namecomments"
        >
          <div class="name">
            <!-- 用户头像 -->
            <div class="topimgs">
              <img :src="item.user.avatarUrl" alt="" />
            </div>
            <!-- 用户昵称和时间 -->
            <div class="right">
              <div class="top">{{ item.user.nickname }}</div>
              <div class="bottomtime">{{ item.timeStr }}</div>
            </div>
            <div class="give">
              <span class="Liketimes">{{ item.likedCount }}</span>
              <span
                @click="isd(item,index)"
                :class="[
                  'iconfont',
                  'dianzan',
                  item.liked ? 'icon-dianzan1' : 'icon-dianzan2',
                ]"
              ></span>
            </div>
          </div>
          <div class="namebox">
            {{ item.content }}
          </div>
        </div>
        <div class="inputbottom">
          <input
            @keyup.enter="commentstext"
            v-model="text"
            placeholder="用评论鼓励一下TA吧~"
            type="text"
          />
          <span class="iconfont icon-fasong"></span>
          <span @click="commentstext" class="textspan">发送</span>
        </div>
      </div>
    </van-popup>

    <!-- 迷你播放器 -->
    <div class="mini-player" v-if="SongDetail">
      <div
        @click="showPlayer = true"
        :class="['img', 'play', { pause: iconplay }]"
      >
        <img :src="SongDetail.al.picUrl" alt="" />
      </div>
      <div @click="showPlayer = true" class="text">
        <div class="text-box">
          <h2 :class="['play', { pause: iconplay }]">
            --{{ SongDetail.name }}--
          </h2>
          <h4 :class="['plays', { pauses: iconplay }]">
            --{{ SongDetail.name }}--
          </h4>
        </div>
        <h3>
          <span v-for="(item, index) in SongDetail.ar" :key="index"
            >{{ item.name }}&nbsp;</span
          >
        </h3>
      </div>

      <div class="control" @click.stop="toggleplay">
        <span class="iconfont icon-24gf-play" v-show="iconplay"></span>
        <span class="iconfont icon-24gf-pause2" v-show="!iconplay"></span>
      </div>
      <div class="showlist">
        <!-- 评论 -->
        <div is-link @click="showlist" class="iconfont icon-liebiao_o"></div>
      </div>
      <!-- 弹出歌单列表 -->
      <van-popup
        v-model="showlidt"
        round
        close-icon-position="top-right"
        position="bottom"
        :style="{ height: '50%' }"
      >
        <div
          @click="setId(index)"
          v-for="(item, index) in playerLists"
          :key="item.id"
          class="lidttext"
        >
          <div class="left">{{ index + 1 }}</div>
          <div class="right">
            <div class="top">{{ item.name }}</div>
            <div class="bottom">{{ item.ar[0].name }}</div>
          </div>
        </div>
      </van-popup>
    </div>
    <!-- oncanplay	当媒介能够开始播放但可能因缓冲而需要停止时运行脚本 -->
    <!-- loadeddata 	当加载媒介数据时运行脚本 -->
    <!-- 音频播放器 -->
    <audio
      ref="audio"
      hidden
      controls
      :src="musicData"
      @loadeddata="loadeddataFun"
      @ended="endedFun"
    ></audio>
  </div>
</template>

<script>
import {
  getSongUrl,
  getSongDetail,
  getLyric,
  getPlaylistSubscribe,
  getCommentMusic,
} from "../../api/base";
import { getComment,getCommentLike } from "../../api/userinfo";
import { getPlaylistDetail } from "../../api/rank";
import { Toast } from "vant";
export default {
  props: ["playerId", "listId"],
  data() {
    return {
      musicData: "",
      iconplay: true, //播放按钮
      SongDetail: null, //歌曲信息
      value: 0, //滚动条 0-100
      timeRight: 0, //右侧时间
      showPlayer: true, //全屏播放器状态
      timeLeft: 0, //左侧时间
      clearset: null, //定时器
      playerLists: [], //播放列表
      togglelyric: true, //歌词切换
      lyric: [], //歌词数组
      lyricTop: "200px", //歌词位置
      currentRow: 0, //歌词高亮
      playerType: 1, //播放類型 1.列表播放 2.随机播放
     
      show: false,
      text: null, //评论内容
      commenlist: [], //评论列表
      showlidt: false, //弹出歌单列表
    };
  },
  methods: {
    // 获取歌曲url
    getSongUrlFun() {
      getSongUrl({ id: this.playerId }).then((data) => {
        this.musicData = data.data[0].url;
        this.iconplay = true;
      });
    },
    // 获取歌曲信息
    getSongDetailFun() {
      getSongDetail({ ids: this.playerId }).then((data) => {
        console.log(data);
        this.SongDetail = data.songs[0];
      });
    },
    // 获取歌曲歌词
    getLyricFun() {
      getLyric({ id: this.playerId }).then((data) => {
        console.log(data);
        this.formatLyric(data.lrc.lyric);
      });
    },

    // 处理歌词结构
    formatLyric(text) {
      this.lyric = [];
      let arr = text.split("\n"); //把原歌词切割成字符串
      let row = arr.length; //获取数组长度
      for (let i = 0; i < row; i++) {
        let temp_row = arr[i]; //[00:00.000] 作词 : MK王逸杰
        let temp_arr = temp_row.split("]"); //[00:00.000 , 作词 : MK王逸杰
        let lyrictext = temp_arr.pop(); //删除数组最后一个,返回值
        temp_arr.forEach((el) => {
          console.log(el);
          let obj = {};
          // [00:00.000 => [00,00.000]
          let time_arr = el.substr(1, el.length - 1).split(":");
          let s = time_arr[0] * 60 + Math.ceil(time_arr[1]); //把分钟变成秒
          obj.text = lyrictext;
          obj.time = s;
          if (lyrictext.length > 0) {
            this.lyric.push(obj); //每一行歌词处理后放入数组中
          }
        });
      }
      console.log(this.lyric);
    },
    // 播放暂停音乐
    toggleplay() {
      if (this.iconplay) {
        this.$refs.audio.play(); //播放音乐

        clearInterval(this.clearset); //清除定时器
        this.clearset = setInterval(() => {
          let currentTime = this.$refs.audio.currentTime; //实时变化时间
          let duration = this.$refs.audio.duration; //总时间
          this.timeLeft = currentTime;
          // 计算滚动条值
          this.value = (currentTime / duration) * 100;
          if (this.value == 100) {
            clearInterval(this.clearset);
            this.$refs.audio.pause(); //暂停音乐
            this.iconplay = true;
          }
        }, 999);
      } else {
        this.$refs.audio.pause(); //暂停音乐
        clearInterval(this.clearset);
      }

      this.iconplay = !this.iconplay; //切换页面
    },
    // 音乐加载完成后,获取音乐数据
    loadeddataFun() {
      this.toggleplay(); //自动播放
      let duration = this.$refs.audio.duration;
      this.timeRight = duration;
    },
    // 音乐播放结束后
    endedFun() {
      console.log("音乐播放结束");

      this.iconplay = true;
      // 根据播放音乐类型,选择下一首播放
      if (this.playerType == 1) {
        // 列表播放
        this.lowerFun(); //下一首歌
      } else {
        // 随机播放
        let index = parseInt(Math.random() * this.playerLists.length);
        this.$emit("set-player-id", this.playerLists[index].id);
      }
    },
    // 获取滚动条变化
    sliderchange() {
      var duration = (this.$refs.audio.duration * this.value) / 100;
      this.timeLeft = duration; //修改页面数据
      this.$refs.audio.currentTime = duration; //修改播放时间
    },
    // 下一首歌
    lowerFun(ids) {
      console.log(ids);
      let id = this.SongDetail.id; // 获取id
      // 获取在播放歌曲列表中下标
      let index = this.playerLists.findIndex((item) => item.id == id);
      // 根据播放音乐类型,选择下一首播放
      if (this.playerType == 1) {
        // 1.获取当前歌曲下标,下标加一,下一首歌.如果最后一首歌,重置为零回到第一首歌
        index = index + 1 > this.playerLists.length - 1 ? 0 : index + 1;
      } else {
        // 随机播放
        index = parseInt(Math.random() * this.playerLists.length);
      }
      // 返回父组件歌曲id
      this.$emit("set-player-id", this.playerLists[index].id); //修改父组件id值
    },
    // 上一首歌
    startup() {
      let id = this.SongDetail.id; // 获取id
      // 获取在播放歌曲列表中下标
      let index = this.playerLists.findIndex((item) => item.id == id);
      // 根据播放音乐类型,选择下一首播放
      if (this.playerType == 1) {
        // 1.获取当前歌曲下标,下标减一,上一首歌.如果第一首歌,设置为最后一首歌
        index = index - 1 < 0 ? this.playerLists.length - 1 : index - 1;
      } else {
        // 随机播放
        index = parseInt(Math.random() * this.playerLists.length);
      }
      // 返回父组件歌曲id
      this.$emit("set-player-id", this.playerLists[index].id); //修改父组件id值
    },
    // 播放类型切换
    changeType() {
      this.playerType = this.playerType == 1 ? 2 : 1;
      if (this.playerType == 1) {
        Toast("列表播放");
      } else {
        Toast("随机播放");
      }
    },
    // 收藏部分
    dianzis() {
      this.dianzan = !this.dianzan;
      if (this.dianzan) {
        Toast({
          message: "取消收藏",
          position: "bottom",
          duration: 500,
        });
        getPlaylistSubscribe({ t: 2, id: this.listId || this.playerId }).then(
          (data) => {
            console.log(data);
          }
        );
      } else {
        Toast({
          message: "收藏成功",
          position: "bottom",
          duration: 500,
        });
        getPlaylistSubscribe({ t: 1, id: this.listId || this.playerId }).then(
          (data) => {
            console.log(data);
          }
        );
      }
    },
    // 获取用户评论
    showPopup() {
      this.show = true;
      getCommentMusic({ id: this.playerId }).then((data) => {
        console.log(data.comments);
        this.commenlist = data.comments;
      });
    },
    //判断点赞
    // 判断点赞
    isd(item,index) {
      // this.isdia = !this.isdia;
      if (item.liked) {
        this.$set(item, (item.liked = false));
        Toast({
          message: "取消点赞",
          position: "bottom",
          duration: 500,
        });
        // item.likedCount -= 1;
        getCommentLike({ t: 0, id:this.playerId, cid: this.commenlist[index].commentId }).then((data) => {
          console.log(data);
        });
      } else {
        this.$set(item, (item.liked = true));
        Toast({
          message: "点赞成功",
          position: "bottom",
          duration: 500,
        });
        // item.likedCount += 1;
        getCommentLike({ t: 1, id:this.playerId, cid: this.commenlist[index].commentId }).then((data) => {
          console.log(data);
        });
      }
    },
    commentstext() {
      getComment({ t: 1, type: 0, id: this.playerId, content: this.text }).then(
        (data) => {
          console.log(data);
          this.text = "";
          Toast({
            message: "发布成功",
            position: "bottom",
            duration: 500,
          });
        }
      );
    },
    showlist() {
      this.showlidt = true;
    },
    setId(index) {
      this.$emit("set-player-id", this.playerLists[index].id);
    },
  },
  // 侦听器,只要数据修改,重新执行
  watch: {
    // 侦听播放id
    playerId() {
      this.getSongUrlFun(); //歌曲url
      this.getSongDetailFun(); //歌曲信息
      this.getLyricFun(); //歌曲歌词
    },
    // 侦听播放列表id
    listId() {
      console.log("歌单数据");
      getPlaylistDetail({ id: this.listId }).then((data) => {
        console.log(data);
        if (data) {
          this.playerLists = data.playlist.tracks; //获取歌单数据
          console.log(this.playerLists);
        } else {
          return;
        }
      });
    },
    // 左测歌时间变化
    timeLeft() {
      for (let i = 0; i < this.lyric.length; i++) {
        if (parseInt(this.timeLeft) <= this.lyric[i].time) {
          this.lyricTop = 200 - i * 36 + "px";
          this.currentRow = i; //歌词高亮
          break;
        }
      }
    },
  },
  filters: {
    playerTime(data) {
      let m = parseInt(data / 60); //分钟
      let s = parseInt(data % 60); //秒
      if (m == 0 && s == 0) {
        return "0:00";
      } else {
        return m + ":" + (s < 10 ? "0" + s : s); // 01
      }
    },
  },
};
</script>

<style lang="less">
.player {
  position: fixed;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 1);
  color: #fff;
  z-index: 2;
  .bg {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    // background-image: url("https://p2.music.126.net/e5cvcdgeosDKTDrkTfZXnQ==/109951166155165682.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    background-position: 50%;
    transform: scale(1.8);
    transform-origin: center center;
    filter: blur(30px);
    opacity: 1;
    z-index: 3;
  }
  .shade {
    position: absolute;
    z-index: 4;
    left: 0px;
    bottom: 0px;
    height: 50%;
    width: 100%;
    background-image: linear-gradient(180deg, rgba(0, 0, 0, 0), #000);
  }
  .view {
    position: absolute;
    z-index: 5;
    left: 0px;
    bottom: 0px;
    height: 100%;
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
    .button {
      font-size: 22px;
      position: absolute;
      left: 13px;
      top: 13px;
      font-weight: bold;
    }
    .title {
      margin: 0 auto;
      text-align: center;
      width: 200px;
      padding-top: 5px;
      h2 {
        font-size: 20px;
      }
      h3 {
        font-size: 14px;
      }
    }
    .content {
      text-align: center;
      height: 65%;
      width: 100%;
      position: relative;
      .img {
        position: absolute;
        top: 0px;
        left: 0px;
        right: 0px;
        bottom: 0px;
        margin: auto;
        width: 300px;
        height: 300px;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.3);
        img {
          margin: 0 auto;
          width: 274px;
          height: 274px;
          border-radius: 50%;
          padding-top: 13px;
        }

        &.play {
          animation: rotate 20s linear infinite;
        }
        &.pause {
          animation-play-state: paused;
        }
      }
      .lyrics {
        width: 100%;
        height: 100%;
        position: relative;
        overflow: hidden;
        ul {
          position: absolute;
          width: 100%;
          top: 180px;
          left: 0px;
          transition: all 0.3s linear;
          flex-direction: column;
          li {
            height: 36px;
            line-height: 36px;
            color: #cbcbcb;
            font-size: 16px;
            overflow: hidden;
            &.con {
              color: #fff;
            }
          }
        }
      }
    }
  }
  .shareviedo {
    width: 100%;
    height: 24px;
    top: 73%;
    position: absolute;
    div {
      position: absolute;
      line-height: 24px;
      font-size: 24px;
      right: 5%;
    }
  }
  .range {
    position: absolute;
    width: 100%;
    left: 0px;
    bottom: 110px;
    text-align: center;
    .bar {
      width: 66%;
      margin: auto;
      position: relative;
      font-size: 14px;
      .timeLeft {
        position: absolute;
        left: -34px;
        top: -6px;
      }
      .timeRight {
        position: absolute;
        right: -34px;
        top: -6px;
      }
    }
  }
  .buttons {
    display: flex;
    position: absolute;
    bottom: 40px;
    width: 100%;
    span {
      width: 45px;
      height: 45px;
      color: white;
    }
    .iconfont {
      flex: 1;
      width: 20%;
      text-align: center;
      font-size: 24px;
      line-height: 38px;
      color: #fff;
    }
    .icon-shixin {
      color: red;
    }
  }
}

.custom-button {
  width: 6px;
  height: 6px;
  background-color: #d0261a;
  border: 5px solid #fff;
  border-radius: 50%;
}

.mini-player {
  position: fixed;
  left: 0px;
  bottom: 0px;
  width: 100%;
  height: 52px;
  background-color: #ececece6;
  display: flex;
  z-index: 1;
  .showlist {
    width: 45px;
    height: 45px;
    div {
      font-size: 30px;
      text-align: center;
      line-height: 45px;
      padding-top: 5px;
    }
  }
  .lidttext {
    display: flex;
    width: 100%;
    padding-top: 20px;
    .left {
      flex: 0 0 44px;
      line-height: 53px;
      text-align: center;
      font-size: 16px;
      color: #6d6e6e;
    }
    .right {
      width: 100%;
      border-bottom: 1px solid #e4e4e4;
      .top {
        padding-top: 5px;
        font-size: 16px;
        color: #1b1c1c;
        height: 22px;
        overflow: hidden;
      }
      .bottom {
        font-size: 12px;
        color: #929393;
      }
    }
  }
  .img {
    height: 37px;
    width: 37px;
    margin: 7px 9px 0px 17px;
    background-color: aqua;
    border-radius: 50%;
    img {
      height: 37px;
      width: 37px;
      border-radius: 50%;
    }
    &.play {
      animation: rotate 20s linear infinite;
    }
    &.pause {
      animation-play-state: paused;
    }
  }
  .text {
    flex: 1;
    .text-box {
      width: 200px;
      height: 28px;
      display: flex;
      position: relative;
      overflow: hidden;
      h2 {
        font-size: 16px;
        line-height: 16px;
        color: #131314;
        margin-top: 12px;
        height: 17px;
        position: absolute;
        // overflow: hidden;
        &.play {
          animation: translate 5s linear infinite;
        }
        &.pause {
          animation-play-state: paused;
        }
      }
      h4 {
        position: absolute;
        font-size: 16px;
        line-height: 16px;
        color: #131314;
        margin-top: 12px;
        height: 17px;
        &.plays {
          animation: translates 5s linear infinite;
        }
        &.pauses {
          animation-play-state: paused;
        }
      }
    }
    h3 {
      line-height: 12px;
      font-size: 12px;
      color: #929393;
      padding-top: 3px;
    }
  }
  .control {
    text-align: center;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    line-height: 26px;
    border: 2px solid #464848;
    margin: 11px 14px 0 0;
    .iconfont {
      color: #464848;
      font-size: 12px;
    }
  }
}

.commentsvie {
  width: 100%;
  .mvcomments {
    display: flex;
    justify-content: space-between;
    div {
      padding: 19px;
      font-weight: 800;
      font-size: 16px;
    }
    .tuijian {
      padding: 19px 50px;
      font-size: 14px;
      font-weight: 800;
      color: #5f5f5f;
    }
  }
  p {
    width: calc(100% - 10px);
    padding: 5px 0px 5px 10px;
    color: #666;
    background-color: #eee;
    font-size: 12px;
  }
  .namecomments {
    width: 100%;
    .name {
      width: 100%;
      display: flex;
      position: relative;
      .topimgs {
        padding: 10px;
        width: 35px;
        height: 35px;
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
        }
      }
      .right {
        display: flex;
        flex-direction: column;
        justify-content: center;
        .top {
          font-size: 14px;
          color: #666;
        }
        .bottomtime {
          font-size: 12px;
          padding-top: 3px;
          color: #999;
        }
      }
      .give {
        position: absolute;
        top: 10px;
        right: 10px;
        .Liketimes {
          font-size: 12px;
          color: #999;
          padding-right: 5px;
        }
        span {
          line-height: 12px;
        }
        .dianzan {
          font-size: 17px;
        }
      }
    }
    .namebox {
      color: #444343;
      margin-left: 54px;
      font-size: 15px;
      padding-bottom: 15px;
      margin-right: 5px;
      border-bottom: 1px solid #f1f2f3;
    }
    padding-bottom: 10px;
  }
  .inputbottom {
    position: fixed;
    width: 100%;
    bottom: 0px;
    height: 45px;
    border-top: 1px solid #fbf4f4;
    background-color: white;
    input {
      width: 75%;
      height: 45px;
      margin-left: 5%;
      border: transparent;
      color: #666;
    }
    ::-webkit-input-placeholder {
      color: #dedede;
    }
    span {
      display: inline-block;
      height: 45px;
      line-height: 45px;
      padding: 0px 5px;
    }
    .textspan {
      font-size: 14px;
      color: #999;
    }
  }
  margin-bottom: 45px;
}

@keyframes rotate {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes translate {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(-200px);
  }
}
@keyframes translates {
  0% {
    transform: translateX(200px);
  }
  100% {
    transform: translateX(0px);
  }
}
</style>